package com.xcity.game.npc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.xcity.game.combat.Combat;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.unit.CombatableBase;
import com.xcity.game.skill.template.SkillTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.util.CommonUtils;

public class NpcCombatable implements CombatableBase {
	
	private NpcTemplate template;
	private float[] attributes;
	private List<NpcSkill> skills;
	
	public NpcCombatable(NpcTemplate template) {
		this.template = template;
	}

	public NpcTemplate toTemplate() {
		return template;
	}

	@Override
	public long getId() {
		return template.getId();
	}

	@Override
	public byte getSex() {
		return 0;
	}

	@Override
	public String getName() {
		return template.getName();
	}

	@Override
	public String getIcon() {
		return template.getIcon();
	}

	@Override
	public int getLevel() {
		return template.getLevel();
	}

//	@Override
//	public float getSTR() {
//		return template.getLevel() * 2;
//	}
//
//	@Override
//	public float getSTA() {
//		return template.getLevel() * 2;
//	}
//
//	@Override
//	public float getSPI() {
//		return template.getLevel() * 2;
//	}
//
//	@Override
//	public float getINT() {
//		return template.getLevel() * 2;
//	}
//
//	@Override
//	public float getAGI() {
//		return template.getLevel() * 2;
//	}

	@Override
	public float setAttributeValue(CombatAttributeType type, float value) {
		if (attributes == null) {
			attributes = new float[CombatAttributeType.length()];
			Arrays.fill(attributes, -1f);
		}
		attributes[type.ordinal()] = value;
		return value;
	}
	
	@Override
	public float getAttributeValue(CombatAttributeType type) {
		float val = 0f;
		if (attributes != null && (val = attributes[type.ordinal()]) != -1f) {
			return val;
		}
		NpcTemplate template = this.template;
		if (type == CombatAttributeType.ATTACK_POWER) {
			val = CommonUtils.randomInt(Combat.RND, template.getMinAttackPower(), template.getMaxAttackPower());
		} else if (type == CombatAttributeType.MAGIC_POWER) {
			val = CommonUtils.randomInt(Combat.RND, template.getMinMagicPower(), template.getMaxMagicPower());
		} else if (type == CombatAttributeType.DEFENSE) {
			val = CommonUtils.randomInt(Combat.RND, template.getMinDefense(), template.getMaxDefense());
		} else if (type == CombatAttributeType.MAGIC_DEFENSE) {
			val = CommonUtils.randomInt(Combat.RND, template.getMinMagicDefense(), template.getMaxMagicDefense());
		} else if (type == CombatAttributeType.CRIT) {
			val = template.getCrit();
		} else if (type == CombatAttributeType.AVOID_CRIT) {
			val = template.getAvoidCrit();
		} else if (type == CombatAttributeType.ATTACK_SPEED) {
			val = template.getAttackSpeed();
		} /*else if (type == CombatAttributeType.CRIT_DAMAGE) {
			val = template.getCritDamage();
		} */else if (type == CombatAttributeType.MAX_HP) {
//			val = CommonUtils.randomInt(Combat.RND, template.getMinHp(), template.getMaxHp());
			val = template.getMaxHp(); // hp, maxHp都取最大值，避免准备战斗界面显示不一致
		} else if (type == CombatAttributeType.HP) {
//			val = CommonUtils.randomInt(Combat.RND, template.getMinHp(), template.getMaxHp());
			val = template.getMaxHp();
		} /*else if (type == CombatAttributeType.MP) {
		} else if (type == CombatAttributeType.MAX_MP) {
		}*/else if (type == CombatAttributeType.DODGE) {
			val = template.getDodge();
		} else if (type == CombatAttributeType.RESIST) {
			val = template.getResist();
		} else if (type == CombatAttributeType.HIT_RATE) {
			val = template.getHitRate();
		} else if (type == CombatAttributeType.MAGIC_HIT_RATE) {
			val = template.getMagicHitRate();
		}
		return val;
	}

	@Override
	public List<NpcSkill> getSkills() {
		if (skills == null) {
			int[] skillIds = template.getSkillIds();
			if (skillIds != null && skillIds.length > 0) {
				skills = new ArrayList<NpcSkill>(skillIds.length);
				TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
				for (int id : skillIds) {
					SkillTemplate st = service.get(SkillTemplate.class, id);
					skills.add(new NpcSkill(st));
				}
			} else {
				skills = Collections.emptyList();
			}
		}
		return skills;
	}
}